package cn.bossx.org.controller;

import cn.bossx.basic.util.AjaxResult;
import cn.bossx.basic.util.PageList;
import cn.bossx.org.domain.Shop;
import cn.bossx.org.query.ShopQuery;
import cn.bossx.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理接口
 * 要求前后端接口对接的数据传输格式：必须是json格式
 *
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    //新增或修改   POST
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺注册")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        }  catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    //新增或修改   POST
    @PostMapping("/save")
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 写接口的三要素：
     *   1.请求地址是什么
     *   2.请求的参数是什么
     *   3.返回值是什么
     * @param id
     * @return
     */
    //删除    DELETE
    @DeleteMapping("/{id}")  // http://localhost:8080/dept/1
    public AjaxResult removeById(@PathVariable("id") Long id){

        /* low
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult();
            result.setSuccess(false);
            result.setMessage("系统繁忙,稍后重试");
            return result;
        }*/
        /* 高级玩家 */
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }
    }

    //获取一条数据   GET
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    //获取所有数据   GET
    @GetMapping("/list")
    public List<Shop> findAll(){
        return shopService.queryAll();
    }

    //分页 + 高级查询   POST
    @PostMapping("/page")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 批量删除
     *  如果前端传的是 数组,那么在后端可以使用 list 或者 数组 接收
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }

    }


}
